﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Core;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;
using ZhouYu.NewWorld.Web.Models;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class UserInfoController : Controller
    {

        //注入
        private IUserService _userService;
        public UserInfoController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 用户列表查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult GetUserList(int page, int limit)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //定义表数据总数变量
            int count = 0;
            Expression<Func<UserInfo, bool>> lambdaCategory = a => true;
            Expression<Func<UserInfo, int>> orderCategory = a => a.User_Id;
            //创建List集合接收业务层返回值
            List<UserInfo> userInfos = _userService.QueryPageList(page, limit, lambdaCategory, orderCategory, out count);
            //赋值
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            responseResult.data = userInfos.Skip((page - 1) * limit).Take(limit).ToList();
            responseResult.count = count;
            return Json(responseResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 添加或编辑用户信息
        /// </summary>
        /// <param name="userInfos"></param>
        /// <returns></returns>
        public ActionResult UserInfoAddOrEdit(UserInfo userInfos)
        {
            //创建操作类对象
            Result operateResult = new Result();
            if (userInfos.User_Id == 0)
            {
                UserInfo userInfo = new UserInfo();
                userInfo.User_VoucherCount = 0;
                userInfo.User_TravelVoCount = 0;
                userInfo.User_Name = userInfos.User_Name;
                userInfo.User_Pwd = userInfos.User_Pwd;
                userInfo.User_Email = userInfos.User_Email;
                userInfo.User_Telphone = userInfos.User_Telphone;
                userInfo.User_Adress = userInfos.User_Adress;
                userInfo.User_NickName = userInfos.User_NickName;
                userInfo.User_TravelVoCount = 0;
                userInfo.User_VoucherCount = 0;
                userInfo.User_CreateTime = DateTime.Now;
                userInfo.User_LoginTime = DateTime.Now;
                operateResult.status = _userService.Add(userInfo) ? "y" : "n";
                if (operateResult.status == "y")
                {
                    operateResult.info = "添加成功";
                }
                else
                {
                    operateResult.info = "添加失败";
                }
            }
            else
            {
                //更新
                Expression<Func<UserInfo, bool>> whereLambda = a => a.User_Id == userInfos.User_Id;
                UserInfo userInfo = _userService.Query(whereLambda); 
                userInfo.User_Pwd = userInfos.User_Pwd;
                userInfo.User_Email = userInfos.User_Email;
                userInfo.User_Telphone = userInfos.User_Telphone;
                userInfo.User_Adress = userInfos.User_Adress;
                userInfo.User_NickName = userInfos.User_NickName;
                userInfo.User_UpdateTime = DateTime.Now;
                //调用业务层添加方法
                operateResult.status = _userService.Update(userInfo) ? "y" : "n";
                if (operateResult.status == "y")
                {
                    operateResult.info = "编辑成功";
                }
                else
                {
                    operateResult.info = "编辑失败";
                }
                
            }
            return Json(operateResult);
        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        public JsonResult DelUser(UserInfo userInfo)
        {

            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            /*AdminInfoService infoService = new AdminInfoService();*/
            //调用修改密码方法
            responseResult.Success = _userService.Delete(userInfo);
            return Json(responseResult);
        }

    }
}